package com.wandian.weather.mvvm.nlmodern;

import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.reflect.TypeToken;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ScreenUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.R;
import com.wandian.weather.bean.ModernTextBean;
import com.wandian.weather.databinding.WeatherActivityWatherNlModernBinding;
import com.wandian.weather.tools.wnl.SideBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.mylib.libcore.utils.JsonUtil.fromJson;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 农历-现代文
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_WEATHER_NL_MODERN)
public class WeatherNLModernActivity extends BaseMVVMActivity<WeatherNLModernViewModel>  {
    private WeatherActivityWatherNlModernBinding dataBinding;
    private List<ModernTextBean> list = new ArrayList<>();
    private List<String> listStr = new ArrayList<>();
    private RecyclerView recyclerview;
    private SideBar sideBar;
    private int oldChoose = 0;


    private int selId;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.weather_activity_wather_nl_modern);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);

        initClink();
        initRecycle();
        initViewData();
    }


    private void initViewData(){
        dataBinding.bindSysToolBar.toolBarTvTitle.setText("农历-现代文");
        recyclerview = dataBinding.recyclerview;
        sideBar = dataBinding.sideBar;
        Bundle bundle =getIntent().getExtras();
        try {
            if(!UIUtils.isEmpty(bundle)){
                String listJson = bundle.getString("list");
                String strJson= bundle.getString("str");
                if(!UIUtils.isEmpty(listJson)){
                    list = (List<ModernTextBean>) fromJson(listJson, new TypeToken<ArrayList<ModernTextBean>>() {});
                }
                if(!UIUtils.isEmpty(strJson)){
                    listStr = (List<String>) fromJson(strJson, new TypeToken<ArrayList<String>>() {});
                }
                dataBinding.bindSysToolBar.toolBarTvTitle.setText(bundle.getString("data"));
                selId = getIntent().getIntExtra("selId",0);
            }
        }catch (Exception e){

        }

        MyAdapter mMyAdapter = new MyAdapter(list);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        //创建默认的线性LayoutManager
        recyclerview.setLayoutManager(mLayoutManager);
        recyclerview.setAdapter(mMyAdapter);
        sideBar.setData(listStr);
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(int position) {
                if (position != -1) {
                    ((LinearLayoutManager) mLayoutManager).scrollToPositionWithOffset(position, 0);
                }
            }
        });
        recyclerview.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                TextView titleTextView2 = (TextView) recyclerview.getChildAt(0).findViewById(R.id.title);
                for (int i = 0; i < listStr.size(); i++) {
                    String string = listStr.get(i);
                    if (titleTextView2.getText().toString().trim().equals(string) && oldChoose != i) {
                        sideBar.setChoose(i);
                        sideBar.invalidate();
                        oldChoose = i;
                        break;
                    }
                }

            }
        });
        sideBar.setChoose(selId);
        ((LinearLayoutManager) mLayoutManager).scrollToPositionWithOffset(selId, 0);
    }

    private void initRecycle(){
    }
    private void initClink(){
        dataBinding.bindSysToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });
    }

    /***
     * MyAdapter
     */
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        public List<ModernTextBean> datas = null;
        private HashMap<Integer, LinearLayout> listView = null;
        private HashMap<Integer, LinearLayout> listView1 = null;

        public MyAdapter(List<ModernTextBean> datas) {
            this.datas = datas;
            listView = new HashMap<>();
            listView1 = new HashMap<>();
        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = null;
            ViewHolder holder = null;
            switch (viewType) {
                case 1:
                    view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.weather_item_moderntext, viewGroup, false);
                    holder = new ViewHolder(view);
                    break;
                case 2:
                    view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.weather_item_moderntext2, viewGroup, false);
                    holder = new ViewHolder(view);
                    break;
            }
            return holder;
        }

        @Override
        public int getItemViewType(int position) {
            if (this.datas.get(position).getItems2().size() == 0) {
                return 1; // 显示一种视图
            } else {
                return 2;// 显示多列的视图
            }
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            switch (getItemViewType(position)) {
                case 1:
                    onBindViewHolder1(viewHolder, position);
                    break;
                case 2:
                    onBindViewHolder2(viewHolder, position);
                    break;
            }
        }

        /**
         * 一个视图的情况
         *
         * @param viewHolder
         * @param position
         */
        private void onBindViewHolder1(ViewHolder viewHolder, int position) {
            viewHolder.titleTextView.setText(listStr.get(position));
            viewHolder.lly_content.removeAllViews();
//            viewHolder.lly_content.destroyDrawingCache();

//            LinearLayout view = listView.get(position);
//            if (view == null) {
            LinearLayout view = new LinearLayout(mContext);
            view.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams params0 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(params0);
            view.setPadding(5, 0, 45, 0);


            List<ModernTextBean.ItemValue> list = datas.get(position).getItems();
            for (ModernTextBean.ItemValue item : list) {
                if (item.getKey() != null && item.getKey().trim().length() > 0) {
                    TextView txTitle = new TextView(mContext);
                    txTitle.setText(item.getKey());
                    txTitle.setLineSpacing(0f,1.2f);
                    txTitle.setTextColor(Color.parseColor("#6F77FF"));
                    txTitle.setPadding(0, 15, 0, 0);
                    txTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    view.addView(txTitle);
                }
                ////
                if (item.getValue() != null && item.getValue().trim().length() > 0) {
                    TextView txValue = new TextView(mContext);
                    txValue.setText(item.getValue());
                    txValue.setLineSpacing(0f,1.2f);
                    txValue.setPadding(0, 15, 0, 0);
                    txValue.setTextColor(Color.parseColor("#333333"));
                    txValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    view.addView(txValue);
                }
//                listView.put(position, view);
            }

            // 最后了吗
            if (datas.size() == (position + 1)) {
                LinearLayout.LayoutParams params00 = (LinearLayout.LayoutParams) view.getLayoutParams();
                params00.height = recyclerview.getHeight();
                params00.topMargin = ScreenUtil.dp2px(mContext, 10);
                params00.leftMargin = ScreenUtil.dp2px(mContext, 5);
                params00.rightMargin = ScreenUtil.dp2px(mContext, 5);
                viewHolder.view.setLayoutParams(params00);
            } else {

                LinearLayout.LayoutParams params22 = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                params22.topMargin = ScreenUtil.dp2px(mContext, 10);
                params22.leftMargin = ScreenUtil.dp2px(mContext, 5);
                params22.rightMargin = ScreenUtil.dp2px(mContext, 5);
                viewHolder.view.setLayoutParams(params22);
            }
            viewHolder.lly_content.addView(view);
        }


        /**
         * 二个视图的情况
         *
         * @param viewHolder
         * @param position
         */
        private void onBindViewHolder2(ViewHolder viewHolder, int position) {

            TextView textView = (TextView) viewHolder.view.findViewById(R.id.title);
            LinearLayout lly_content1 = (LinearLayout) viewHolder.view.findViewById(R.id.lly_content1);
            LinearLayout lly_content2 = (LinearLayout) viewHolder.view.findViewById(R.id.lly_content2);

            TextView yi_icon = (TextView) viewHolder.view.findViewById(R.id.yi_icon);
            TextView ji_icon = (TextView) viewHolder.view.findViewById(R.id.ji_icon);




            textView.setText(listStr.get(position));

            lly_content1.removeAllViews();
//            lly_content1.destroyDrawingCache();
            lly_content2.removeAllViews();

//            if (view == null) {
            LinearLayout view = new LinearLayout(mContext);
            view.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams params0 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(params0);
            LinearLayout view2 = new LinearLayout(mContext);
            view2.setOrientation(LinearLayout.VERTICAL);
            view2.setLayoutParams(params0);

            view.setPadding(0, 0, 45, 32);
            view2.setPadding(0, 0, 45, 32);

            List<ModernTextBean.ItemValue> list = datas.get(position).getItems();
            for (ModernTextBean.ItemValue item : list) {
                if (item.getKey().trim().length() > 0) {
                    TextView txTitle = new TextView(mContext);
                    txTitle.setText(item.getKey());
                    txTitle.setLineSpacing(0f,1.2f);
                    txTitle.setTextColor(Color.parseColor("#88C613"));
                    txTitle.setPadding(0, 24, 0, 0);
                    txTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    view.addView(txTitle);
                }
                ////
                if (item.getValue() != null && item.getValue().trim().length() > 0) {
                    TextView txValue = new TextView(mContext);
                    txValue.setText(item.getValue());
                    txValue.setLineSpacing(0f,1.2f);
                    txValue.setPadding(0, 15, 0, 0);
                    txValue.setTextColor(Color.parseColor("#666666"));
                    txValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    view.addView(txValue);
                }
//                listView1.put(position, view);
            }
            List<ModernTextBean.ItemValue> list2 = datas.get(position).getItems2();
            for (ModernTextBean.ItemValue item : list2) {
                if (item.getKey().trim().length() > 0) {
                    TextView txTitle = new TextView(mContext);
                    txTitle.setText(item.getKey());
                    txTitle.setLineSpacing(0f,1.2f);
                    txTitle.setTextColor(Color.parseColor("#F23F77"));
                    txTitle.setPadding(0, 24, 0, 0);
                    txTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    view2.addView(txTitle);
                }
                ////
                if (item.getValue() != null && item.getValue().trim().length() > 0) {
                    TextView txValue = new TextView(mContext);
                    txValue.setText(item.getValue());
                    txValue.setLineSpacing(0f,1.2f);
                    txValue.setPadding(0, 15, 0, 0);
                    txValue.setTextColor(Color.parseColor("#666666"));
                    txValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    view2.addView(txValue);
                }
//                listView2.put(position, view);
            }
            lly_content1.addView(view);
            lly_content2.addView(view2);
        }


        //获取数据的数量
        @Override
        public int getItemCount() {
            if (datas == null)
                return 0;
            return datas.size();
        }

        //自定义的ViewHolder，持有每个Item的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            public TextView titleTextView;
            public LinearLayout lly_content;
            public View view;

            public ViewHolder(View view) {
                super(view);
                this.view = view;
                titleTextView = (TextView) view.findViewById(R.id.title);
                lly_content = (LinearLayout) view.findViewById(R.id.lly_content);
            }
        }


    }

}
